package com.mqtt.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mqtt.system.domain.IOTData;
import com.mqtt.system.domain.vo.IOTDataPage;
import com.mqtt.system.domain.vo.IOTDataPageResult;
import com.mqtt.system.domain.vo.UpdateRecords;
import com.mqtt.system.mapper.IOTDataMapper;
import com.mqtt.system.service.ISIOTService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author wang
 * @version 1.0
 * @data 2024/3/26 下午 8:02
 * @desc 描述信息
 */
@Service
public class IOTDataServiceImpl implements ISIOTService {
    @Resource
    private IOTDataMapper iotDataMapper;
    @Override
    public List<IOTData> getListByProperties(String properties,Long page,Long pageSize) {
        QueryWrapper<IOTData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("properties",properties);
        queryWrapper.orderByDesc("create_time");
        Page page1 = Page.of(page - 1,(page) *pageSize );
        Page page11 = iotDataMapper.selectPage(page1, queryWrapper);
        List<IOTData> records = page11.getRecords();
        return records;
    }

    @Override
    public IOTData getOneData(String properties) {
        QueryWrapper<IOTData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("properties",properties);
        queryWrapper.orderByDesc("create_time");
        Page page1 = Page.of(0,1);

        Page page11 = iotDataMapper.selectPage(page1, queryWrapper);
        List<IOTData> records = page11.getRecords();
        if (records.size() > 0){
            return records.get(0);
        }
        return null;
    }

    @Override
    public List<IOTData> queryProperty() {
        QueryWrapper  queryWrapper = new QueryWrapper();
        // queryWrapper.o
        List<IOTData> iotData = iotDataMapper.selectFirstRecordForEachGroupUsingNativeSql();
        return iotData;
    }

    @Override
    public IOTDataPageResult pageList(Long page, Long pageSize) {
        List<IOTDataPage> iotDataPages = iotDataMapper.pageSelect(page, pageSize);
        HashMap<Long,List<IOTDataPage>> map = new HashMap<>();
        for (IOTDataPage iotDataPage : iotDataPages) {
            List<IOTDataPage> iotDataTemp1 = map.get(iotDataPage.getRn());
            if (iotDataTemp1 == null) {
                List<IOTDataPage> temp = new ArrayList<>();
                temp.add(iotDataPage);
                map.put(iotDataPage.getRn(), temp);
            }else {
                iotDataTemp1.add(iotDataPage);
            }
        }
        System.out.println(map.get(1L));
        List<List<IOTDataPage>> list = new ArrayList<>();
        for (Long i = (page-1)* pageSize; i < map.size()+(page-1)* pageSize; i++) {
            List<IOTDataPage> temp = map.get(i+1);
            list.add(temp);
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("properties","H");
        Long aLong = iotDataMapper.selectCount(queryWrapper);
        IOTDataPageResult iotDataPageResult = new IOTDataPageResult();
        iotDataPageResult.setList(list);
        iotDataPageResult.setPageSize(pageSize);
        iotDataPageResult.setTotal(aLong);
        iotDataPageResult.setPage(page);
        return  iotDataPageResult;
    }

    @Override
    public Boolean deleteRecords(List<Long> ids) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("id",ids);
        return iotDataMapper.delete(queryWrapper) > 0;
    }

    @Override
    public Boolean updateRecords(List<IOTData> records) {
        for (int i = 0; i < records.size(); i++) {
            IOTData updateRecords = records.get(i);
            iotDataMapper.updateById(updateRecords);
        }
        return true;
    }

    @Override
    public Boolean inertRecords(List<IOTData> records) {
        for (int i = 0; i < records.size(); i++) {
            IOTData updateRecords = records.get(i);
            iotDataMapper.insert(updateRecords);
        }
        return true;
    }
}
